/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduleManager;

import app.Global;
import app.MyCellRenderer;
import entities.EmployeeSchedule;
import java.awt.Dimension;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import lib.multiSpanCellTable.AttributiveCellTableModel;
import lib.multiSpanCellTable.CellSpan;

/**
 *
 * @author yoshi
 */
public class ScheduleManagerDefault extends AbstractScheduleManager {

    public ScheduleManagerDefault(long departmentId) {
        super(departmentId);
    }

    @Override
    protected void initTable() {
        ArrayList<EmployeeSchedule> schedules = this.getMyModel().getSchedules();
        Vector vData = new Vector();
        Vector vRow = null;
        Vector vDate = new Vector();
        Vector vTimeSlot = new Vector();
        Vector vDateGroup = null;
        Vector vTimeSlotGroup = new Vector();
        Date currentDate = null;
        long currentTimeSlot = 0;
        int count = 0;

        for (EmployeeSchedule schedule : schedules) {
            /* Init the row data */
            SimpleDateFormat df = Global.getDefaultDateFormat();
            vRow = new Vector(6);
            vRow.add(df.format(new java.util.Date(schedule.getDate().getTime())));
            vRow.add(schedule.getTimeSlot().toFormatString());
            vRow.add(schedule.getEmployee().getUsername());
            vRow.add(schedule.getEmployee().getFullName());
            vRow.add(schedule.getEmployee().getRoll().getName());
            vRow.add(new RequestButtonModel(schedule));
            vData.add(vRow);
            
            /* Merge date cells */
            if (!schedule.getDate().equals(currentDate)) {
                currentDate = schedule.getDate();
                vDateGroup = new Vector();
                vDate.add(vDateGroup);
            }
            vDateGroup.add(count);

            /* Merge time slot cells */
            if (!schedule.getDate().equals(currentDate) || schedule.getTimeSlotId() != currentTimeSlot) {
                currentTimeSlot = schedule.getTimeSlotId();
                vTimeSlotGroup = new Vector();
                vTimeSlot.add(vTimeSlotGroup);
            }
            vTimeSlotGroup.add(count);
            
            count++;
        }

        Vector vHead = new Vector(6);
        vHead.add("Date");
        vHead.add("Time slot");
        vHead.add("Username");
        vHead.add("Name");
        vHead.add("Roll");
        vHead.add("Request");
        
        AttributiveCellTableModel ml = new AttributiveCellTableModel(vData, vHead);
        final CellSpan cellAtt = (CellSpan) ml.getCellAttribute();
        
        table.setModel(ml);
        table.getColumnModel().getColumn(0).setCellRenderer(new MyCellRenderer(SwingConstants.CENTER));
        table.getColumnModel().getColumn(1).setCellRenderer(new MyCellRenderer(SwingConstants.CENTER));
        table.getColumnModel().getColumn(4).setCellRenderer(new MyCellRenderer(SwingConstants.CENTER));
        table.makeButtonColumn(5, new RequestButtonEditor(new JCheckBox(), this), new RequestButtonRenderer());
        table.makeItuneColumn(2);
        table.makeItuneColumn(3);
        table.makeItuneColumn(4);
        
        table.setIntercellSpacing(new Dimension(0, 0));
        table.setShowGrid(false);
        table.setCellSelectionEnabled(false);

        for (Object list : vDate) {
            int[] columns = {0};
            Vector v = (Vector) list;
            int[] rows = new int[v.size()];

            short i = 0;
            for (Object row : v) {
                rows[i++] = (Integer) row;
            }
            cellAtt.combine(rows, columns);
        }

        for (Object list : vTimeSlot) {
            int[] columns = {1};
            Vector v = (Vector) list;
            int[] rows = new int[v.size()];

            short i = 0;
            for (Object row : v) {
                rows[i++] = (Integer) row;
            }
            cellAtt.combine(rows, columns);
        }
    }

    @Override
    protected void initModel() {
        this.getMyModel().initDefault();
    }
}
